#include "bufferapi.h"
#include "strapi.h"


boolean create_buffer(Buffer buf, char *base, int size)
{
    buf->buff = base;
    buf->size = size;
    buf->rptr  = 0;
    buf->wptr  = 0;
    return True;
}

boolean put_to_buffer(Buffer buf, char c)
{
    if(buf->wptr >= buf->size)
    {
        return False;
    }
    *(buf->buff + buf->wptr) = c;
    buf->wptr++;
    return True;
}

char get_from_buffer(Buffer buf)
{
    if(buf->rptr >= buf->wptr)
    {
        return '\0';
    }
    char c = *(buf->buff + buf->rptr);
    buf->rptr++;
    return c;
}

int stream_to_buffer(Buffer buf, char* stream, int size)
{
    int n = 0;
    n = strncpy(stream, buf->buff + buf->rptr, size);
    buf->rptr += n;
    return 0;
}

int stream_fm_buffer(Buffer buf, char* stream, int size)
{
    int space = buf->size - buf->wptr;
    int n = space < size ? space : size;
    strncpy(buf->buff + buf->wptr, stream, n);
    return 0;
}
